package com.ktjy.ch15.controller;

import com.ktjy.ch15.entity.LanguageCategory;
import com.ktjy.ch15.entity.Video;
import com.ktjy.ch15.service.LanguageCategoryService;
import com.ktjy.ch15.service.VideoService;
import com.ktjy.ch15.utils.Constants;
import com.ktjy.ch15.utils.Page;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/main/video")
public class VideoController extends BaseController{
    private static final Logger LOGGER = LoggerFactory.getLogger(VideoController.class);
    @Autowired
    private LanguageCategoryService languageCategoryService;
    @Autowired
    private VideoService videoService;


    @RequestMapping("/page")
    public String page(Model model, Video condition,
                       @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                       @RequestParam(value = "pageSize", defaultValue = "5") Integer
                               pageSize) {
        Page<Video> page = null;
        try {
            // 查询用户的分页信息
            page = videoService.findPage(condition, pageNo, pageSize);
            // 查询所有的角色信息
            List<LanguageCategory> list = languageCategoryService.findAll();
            List<Video> list1 = languageCategoryService.findAllVideo();
            // 将分页数据、角色列表和查询条件设置到模型数据中
            model.addAttribute("pageInfo", page);
            model.addAttribute("list", list);
            model.addAttribute("list1", list1);
            model.addAttribute("condition", condition);
        } catch (Exception e) {
            LOGGER.error("VideoController 中的 page 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "video/page";
    }

    @GetMapping("/save")
    public String toSave(Model model) {
        List<LanguageCategory> list = null;
        List<Video> list1 = null;
        try {
            list = languageCategoryService.findAll();
            list1 = languageCategoryService.findAllVideo();
            // 有角色下拉框需要数据
            model.addAttribute("list", list);
            model.addAttribute("list1", list1);
            // 回显数据时需要 sysUser，这里给个空对象避免出错
            model.addAttribute("video", new Video());
        } catch (Exception e) {
            LOGGER.error("VideoController中的toSave方法执行发生错误。", e);
            throw new RuntimeException(e);
        }
        return "video/save";
    }

    @PostMapping("/save")
    public String doSave(Video video, HttpSession session) {
        try {
            boolean ok =false;
            if(video.getId()==null){//新增
                ok = videoService.save(video);
            }else{//修改
                ok= videoService.update(video);
            }

            if(ok){
                //跳转到列表;重定向到分页方法
                return redirect("/main/video/page");
            }else{
                //跳回新增页面
                return "video/save";
            }
        } catch (Exception e) {
            LOGGER.error("VideoController 中的 save 方法执行发生错误。{} ",e);
            throw new RuntimeException(e);
        }
    }

    @GetMapping("/view/{id}")
    public String view(@PathVariable("id") Long id, Model model){
        Video video = null;
        try {
            video = videoService.findById(id);
            model.addAttribute("video", video);
        } catch (Exception e) {
            LOGGER.error("SysUserController 中的 view 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "video/view";
    }

    @GetMapping("/edit")
    public String toEdit(@RequestParam("id") Long id, Model model) {
        Video video = null;
        List<LanguageCategory> list = null;
        List<Video> list1 = null;
        try {
            video = videoService.findById(id);
            list = languageCategoryService.findAll();
            list1 = languageCategoryService.findAllVideo();
            // 有角色下拉框需要数据
            model.addAttribute("list", list);
            model.addAttribute("list1", list1);
            // 回显数据时需要 sysUser，这里给个空对象避免出错
            model.addAttribute("video", video);
        } catch (Exception e) {
            LOGGER.error("VideoController 中的 toEdit 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "video/save";
    }

    @DeleteMapping("/del/{id}")
    @ResponseBody
    public Object del(@PathVariable("id") Long id) {
        Map<String,Object> map = new HashMap<String,Object>();
        try {
            boolean ok = videoService.delete(id);
            if (ok){
                map.put("code",200);
                map.put("msg","删除成功");
            }else {
                map.put("code",500);
                map.put("msg","删除失败");
            }
        }catch (Exception e){
            LOGGER.info("发生错误",e);
            map.put("code",500);
            map.put("msg","发生异常");
        }
        return map;
    }

    @GetMapping("/titleExist")
    @ResponseBody
    public Object titleExist(@RequestParam("title") String title) {
        Map<String,Object> map = new HashMap<>();
        try {
            Video video = videoService.findByTitle(title);
            if (video != null){
                map.put("exist",1);
            }else {
                map.put("exist",0);
            }
        }catch (Exception e){
            LOGGER.error("VideoController中的titleExist方法执行发生错误", e);
            map.put("exist",-1);
            map.put("error", "出现异常！ ");
        }
        return map;
    }

}
